package com.example.listmoudle.sliper;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.widget.ViewUtils;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author: Mr.You
 * @create: 2022-12-15 15:14
 * @description:
 **/
public class SliperLayoutManager extends RecyclerView.LayoutManager {


    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        detachAndScrapAttachedViews(recycler);

        int count = getItemCount();
        int bottomPosition ;
        if (count<4){
            bottomPosition = 0;
        }else {
            bottomPosition = count- 4;
        }
        for (int i = bottomPosition; i < count; i++) {
            View view = recycler.getViewForPosition(i);
           addView(view);
           measureChildWithMargins(view,0,0);
           int itemWidth = getWidth() - getDecoratedMeasuredWidth(view);
           int itemHeight = getHeight() - getDecoratedMeasuredHeight(view);

           layoutDecoratedWithMargins(view,itemWidth/2,itemHeight/2,
               itemWidth/2+getDecoratedMeasuredWidth(view), itemHeight/2+getDecoratedMeasuredHeight(view) );

           int level = count-i-1;
           if (level>0){
              if (level<4-1){
                  view.setTranslationY((float) (dp2px(view.getContext(), 10)*level));
                  view.setScaleX((float) (1-level*0.05));
                  view.setScaleY((float) (1-level*0.05));
              }else {
                  view.setTranslationY((float) (dp2px(view.getContext(), 10)*(level-1)));
                  view.setScaleX((float) (1-(level-1)*0.05));
                  view.setScaleY((float) (1-(level-1)*0.05));
              }
           }


        }

    }
    public double dp2px(Context context,int dp){
        return context.getResources().getDisplayMetrics().density*dp+0.5;
    }
}
